



package view.jfreechartTest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.block.*;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.ScatterRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.*;

import view.DrawXYFromDatabase;

public class DualAxisDemo1 extends ApplicationFrame
{

	private static ArrayList<XYDataset> datasetList;
	private static ArrayList<XYItemRenderer> rendererList;
//	private static ChartTheme theme = new StandardChartTheme("JFree");
	
    public static ArrayList<XYDataset> getDatasetList() {
		return datasetList;
	}

	public static void setDatasetList(ArrayList<XYDataset> datasetList) {
		DualAxisDemo1.datasetList = datasetList;
	}

	public static ArrayList<XYItemRenderer> getRendererList() {
		return rendererList;
	}

	public static void setRendererList(ArrayList<XYItemRenderer> rendererList) {
		DualAxisDemo1.rendererList = rendererList;
	}

	public DualAxisDemo1(String s, ArrayList<XYDataset> datasetList, ArrayList<XYItemRenderer> rendererList)
    {
        super(s);
        DualAxisDemo1.datasetList = datasetList;
        DualAxisDemo1.rendererList = rendererList;
        JFreeChart jfreechart = createChart(s);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartpanel);
    }

    private static XYDataset createDataset1()
    {
    	return DrawXYFromDatabase.drawXYPlotFromViewSet("merge_onlinepay", "dataset1");

    }

    private static XYDataset createDataset2()
    {
    	return DrawXYFromDatabase.drawXYPlotFromViewSet("merge_pospay", "dataset2");
    	
    }

    private static JFreeChart createChart(String chartName)
    {
    	int datasetSize = datasetList.size();
    	int rendererSize = rendererList.size();
    	if(datasetSize != rendererSize) {
    		System.out.print("dataset与renderer数量不匹配！");
    		return null;
    	}
    	DateAxis domainAxis = new DateAxis("日期");
    	ValueAxis valueAxis = new NumberAxis("金额");
    	domainAxis.setAutoRange(true);
    	domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// x轴标题
		domainAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// x轴坐标上文字
		valueAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));// y轴标题
		valueAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 10));// y轴坐标上文字
    	valueAxis.setAxisLineVisible(true);
    	//XYBarRenderer renderer = new XYBarRenderer();
    	XYPlot xyplot = new XYPlot(datasetList.get(0),domainAxis,valueAxis,rendererList.get(0));
    	xyplot.setDomainGridlinePaint(Color.white);
    	xyplot.setRangeGridlinePaint(Color.white);
    	JFreeChart jfreechart = new JFreeChart(xyplot);
    	
    	//JFreeChart jfreechart = ChartFactory.createXYBarChart("Dual Axis Chart", "time", true, "value", (IntervalXYDataset)createDataset1(),PlotOrientation.VERTICAL, true, true, false);
        jfreechart.setBackgroundPaint(Color.gray);
        jfreechart.setTitle(chartName);
        TextTitle t = jfreechart.getTitle();
		t.setFont(new Font("宋体", Font.BOLD, 40));// 标题文字

        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        
        XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
        
//        XYDataset categorydataset = createDataset2();
//        xyplot.setDataset(2, categorydataset);
//        xyplot.setDataset(1, createDataset1());
        /*
        xyplot.mapDatasetToRangeAxis(1, 0);
        DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
        //dateaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
        NumberAxis numberaxis = new NumberAxis("Secondary");
        xyplot.setRangeAxis(1, numberaxis); */
        
        

        BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
        for(int i = 1; i < datasetSize; i++)
        {
        	rendererList.get(i).setBaseToolTipGenerator(toolTipGenerator);
        	xyplot.setRenderer(i, rendererList.get(i));
        	xyplot.setDataset(i, datasetList.get(i));
        	LegendTitle legendtitle = new LegendTitle(rendererList.get(i));
            legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
            legendtitle.setFrame(new BlockBorder());
            blockcontainer.add(legendtitle);

        }
        
        //图例
        
//        LegendTitle legendtitle1 = new LegendTitle(xyplot.getRenderer(1));
//        legendtitle1.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
//        legendtitle1.setFrame(new BlockBorder());
        //多图例添加
//        blockcontainer.add(legendtitle, RectangleEdge.LEFT);
//        blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
        blockcontainer.add(new EmptyBlock(2000D, 0.0D));
        CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
        compositetitle.setPosition(RectangleEdge.BOTTOM);
        jfreechart.addSubtitle(compositetitle);
//        theme.apply(jfreechart);
        return jfreechart;
    }

//    public static JPanel createDemoPanel()
//    {
//        JFreeChart jfreechart = createChart();
//        return new ChartPanel(jfreechart);
//    }

    public static void main(String args[])
    {
    	ArrayList<XYDataset> datasetList = new ArrayList<XYDataset>();
    	datasetList.add(createDataset1());
    	datasetList.add(createDataset1());
    	datasetList.add(createDataset2());
    	
    	ArrayList<XYItemRenderer> rendererList = new ArrayList<XYItemRenderer>();
    	XYBarRenderer renderer = new XYBarRenderer();
    	XYLineAndShapeRenderer lineandshaperenderer = new XYLineAndShapeRenderer();
    	XYStepRenderer setprenderer = new XYStepRenderer();
    	rendererList.add(renderer);
    	rendererList.add(lineandshaperenderer);
    	rendererList.add(setprenderer);
    	
        DualAxisDemo1 dualaxisdemo1 = new DualAxisDemo1("Dual Axis Demo 1", datasetList, rendererList);
        dualaxisdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(dualaxisdemo1);
        dualaxisdemo1.setVisible(true);
    }
}
